//
//  CJBaseViewController.m
//  DatangLife
//
//  Created by 娜 on 2018/4/19.
//  Copyright © 2018年 DatangLife. All rights reserved.
//

#import "CJBaseViewController.h"
#import "CJCustomNoNetworkEmptyView.h"
#import "CJCustomLoadingAnimationView.h"
#import <YYWebImageManager.h>

#import <YYMemoryCache.h>
@interface CJBaseViewController ()
@property (nonatomic, weak) CJCustomNoNetworkEmptyView *noNetworkEmptyView;
@property (nonatomic, weak) CJCustomLoadingAnimationView *animationView;
@end

@implementation CJBaseViewController
- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    

    
    // 在自定义leftBarButtonItem后添加下面代码就可以完美解决返回手势无效的情况
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]){
        self.navigationController.interactivePopGestureRecognizer.delegate = nil;
    }
    
    
}






- (void)viewDidLoad {
    [super viewDidLoad];
    _isSuccessNet = NO;
    
//    [self.navigationController setNavigationBarHidden:NO animated:NO];
//    [UIView setAnimationsEnabled:YES];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeAll;
    self.page = 1;
    
    [NSNotificationCenter addObserver:self action:@selector(requestSuccessNotification) name:kCJRequestSuccessNotification];
    
    // Do any additional setup after loading the view.
}

- (void)requestSuccessNotification{
    [self hideLoadingAnimation];
}



- (void)dealloc{
    [NSNotificationCenter removeAllObserverForObj:self];
}

- (void)pop{
    if (self.navigationController == nil) {
        return;
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)popToRootVc{
    if (self.navigationController == nil) {
        return;
    }
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)popToVc:(UIViewController *)vc{
    if (![vc isKindOfClass:[UIViewController class]]) {
        return;
    }
    if (self.navigationController == nil) {
        return;
    }
    [self.navigationController popToViewController:vc animated:YES];
}

- (void)dismiss{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)dismissWithCompletion:(void (^)(void))completion{
    [self dismissViewControllerAnimated:YES completion:completion];
}

- (void)presentVc:(UIViewController *)vc{
    if (![vc isKindOfClass:[UIViewController class]]) {
        return;
    }
    [self presentVc:vc completion:nil];
}

- (void)presentVc:(UIViewController *)vc completion:(void (^)(void))completion{
    if (![vc isKindOfClass:[UIViewController class]]) {
        return;
    }
    [self presentViewController:vc animated:YES completion:completion];
}

- (void)pushVc:(UIViewController *)vc{
    if (![vc isKindOfClass:[UIViewController class]]) {
        return;
    }
    if (self.navigationController == nil) {
        return;
    }
    if (vc.hidesBottomBarWhenPushed == NO) {
        vc.hidesBottomBarWhenPushed = YES;
    }
    [self.navigationController pushViewController:vc animated:YES];
    
}

- (void)removeChildVc:(UIViewController *)childVc{
    if (![childVc isKindOfClass:[UIViewController class]]) {
        return;
    }
    [childVc.view removeFromSuperview];
    [childVc willMoveToParentViewController:nil];
    [childVc removeFromParentViewController];
}

- (void)addChildVc:(UIViewController *)childVc{
    if (![childVc isKindOfClass:[UIViewController class]]) {
        return;
    }
    [childVc willMoveToParentViewController:self];
    [self addChildVc:childVc];
    [self.view addSubview:childVc.view];
    childVc.view.frame = self.view.bounds;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
/*
- (CJCustomNoNetworkEmptyView *)noNetworkEmptyView{
    if (!_noNetworkEmptyView) {
        CJCustomNoNetworkEmptyView *emptyView = [[CJCustomNoNetworkEmptyView alloc] init];
        [self.view addSubview:emptyView];
        _noNetworkEmptyView = emptyView;
        WeakSelf(weakSelf);
        [emptyView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(weakSelf.view);
        }];
        emptyView.customNoNetworkEmptyViewDidClickRetryHandle = ^(CJCustomNoNetworkEmptyView *view) {
            [weakSelf loadData];
        };
    }
    return _noNetworkEmptyView;
}
*/
- (void)showLoadingAnimation{
    CJCustomLoadingAnimationView *animation = [[CJCustomLoadingAnimationView alloc] init];
    [animation showInView:self.view];
    _animationView = animation;
    [self.view bringSubviewToFront:animation];
}

- (void)hideLoadingAnimation{
    [_animationView dismiss];
    _animationView = nil;
    _isSuccessNet = YES;
}

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    [self.view bringSubviewToFront:self.animationView];
}

- (void)loadData{
//    [self showLoadingAnimation];
    if (!self.isNetworkReachable) {
//        [self hideLoadingAnimation];
        self.noNetworkEmptyView.hidden = NO;
    }else{
        [self.noNetworkEmptyView removeFromSuperview];
        _noNetworkEmptyView = nil;
    }
}

- (BOOL)isNetworkReachable{
    return [AFNetworkReachabilityManager sharedManager].reachable;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    

    [[[YYWebImageManager sharedManager] cache].memoryCache removeAllObjects];
    
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
